import {
  IProjectItemState,
  IProjectModuleState,
  IProjectsTreeState
} from '../store/projects/types'

export function projects2Tree(data: IProjectItemState[]): IProjectsTreeState[] {
  const projects: IProjectsTreeState[] = []
  for (let index = 0; index < data.length; index++) {
    const element = data[index]
    const root = project2Tree(element)
    projects.push(root)
  }
  return projects
}

export function project2Tree(project: IProjectItemState): IProjectsTreeState {
  const root: IProjectsTreeState = {
    id: project.name,
    label: project.name,
    level: 1,
    isLeaf: false,
    arVersion: project.arVersion,
    inputUrl: project.inputUrl,
    outputUrl: project.outputUrl,
    children: [
      {
        label: '基础配置',
        icon: 'el-icon-document',
        path: 'BaseConfiguration',
        level: 2,
        isLeaf: true
      }
    ]
  }
  const modules: IProjectModuleState[] = project.projectModules
  for (let index = 0; index < modules.length; index++) {
    const element: IProjectModuleState = modules[index]
    const child: IProjectsTreeState = {
      id: element.moduleId,
      label: element.name,
      level: 2,
      isLeaf: false,
      children: [
        {
          label: '模块驱动软件信息',
          icon: 'el-icon-document',
          path: 'ModuleDrives',
          level: 3,
          isLeaf: true
        },
        {
          label: '模块参数配置信息',
          icon: 'el-icon-document',
          path: 'ModuleConfigDef',
          level: 3,
          isLeaf: true
        }
      ]
    }
    root.children?.push(child)
  }
  return root
}
